#!/bin/bash
# _*_ coding:utf-8 _*_
# **********************************************************
# * Author        : 李阳
# * Email         : monogy@163.com
# * Create time   : 2025-10-06 11:41
# * Last modified : 2025-10-06 11:41
# * Filename      : Building.sh
# * Description   : NeoVim编译安装程序
# *********************************************************/

NEOVIM_LOCATION="$HOME/program/neovim"
INSTALL_LOCATION="/usr/local"
CONFIG_LOCATION="$HOME/.dotfiles/nvim"

# git下载
if [ ! -d "${NEOVIM_LOCATION}" ]; then # 如果目录不存在
  git clone https://github.com/neovim/neovim.git "$NEOVIM_LOCATION"
  if [ $? -eq 1 ]; then # 命令执行异常
    echo "neovim下载未成功，请重试..."
    exit 1
  else
    echo "neovim下载成功..."
  fi
fi

# 切到NeoVim目录
cd "$NEOVIM_LOCATION" || exit 2

# 删除build目录
[ -d "$NEOVIM_LOCATION/build" ] && rm -rf $NEOVIM_LOCATION/build

# 编译
make CMAKE_BUILD_TYPE=RelWithDebInfo CMAKE_INSTALL_PREFIX=$INSTALL_LOCATION

if [ $? -eq 0 ]; then # 命令执行正常
  sudo make install
  if [ $? -eq 0 ]; then # 命令执行正常
    tar -cvf $CONFIG_LOCATION/budild-deps.tar $NEOVIM_LOCATION/.deps
    $INSTALL_LOCATION/nvim --version
  else
    echo "安装失败，请重试..."
    exit 4
  fi
else
  echo "编译失败，请重试..."
  exit 3
fi
